﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace depl
{
    public partial class ExpanderBox : UserControl, INotifyPropertyChanged
    {
        private bool _expanded = false;
        private bool _loaded = false;
        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }

        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public bool Expanded
        {
            get
            {
                return _expanded;
            }
            set
            {
                if (value != this._expanded)
                {
                    this._expanded = value;
                    NotifyPropertyChanged();
                }
            }

        }

        public static readonly DependencyProperty CaptionProperty =
          DependencyProperty.Register(
              "Caption",
              typeof(string),
              typeof(ExpanderBox),
              new PropertyMetadata(
                  ""
              )
          );

        public static readonly DependencyProperty ContentProperty =
        DependencyProperty.Register(
          "Content", 
          typeof(string),
          typeof(ExpanderBox),
          new PropertyMetadata(
              ""
          )
        );
        public ExpanderBox()
        {
            InitializeComponent();
            CaptionText.DataContext = this;
            HtmlText.DataContext = this;     
        }

        private void TitleOnTap(object sender, System.Windows.Input.GestureEventArgs e)
        {

        }

        private void ShowAnimation()
        {

            AnimationShow.From = 0;
            AnimationShow.To = HtmlText.ActualHeight;
            ShowContent.Begin();

        }
        private void HideAnimation()
        {

            AnimationShow.From = HtmlText.ActualHeight;
            AnimationShow.To = 0;
            ShowContent.Begin();

        }

        private void HeaderClicked(object sender, RoutedEventArgs e)
        {

        }

        private void HeaderOnTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (Expanded)
            {
                Expanded = false;
                HideAnimation();
            }
            else
            {
                Expanded = true;
                if (!_loaded)
                {
                    HtmlText.SizeChanged += HtmlBoxLoaded;
                    HtmlText.HtmlText = Content;
                    
                }
                else
                {
                    ShowAnimation();
                }
                
                
            }
        }

        private void HtmlBoxLoaded(object sender, EventArgs e)
        {
            ShowAnimation();
            _loaded = true;
            HtmlText.LayoutUpdated -= HtmlBoxLoaded;
        }
    }
}
